package com.my.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.my.interceptor.LocalLanguageInterceptor;

import ch.qos.logback.ext.spring.web.WebLogbackConfigurer;

public class LogbackListener implements ServletContextListener{
	private static final Logger logger = LoggerFactory.getLogger(LogbackListener.class);

	@Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContext sc = event.getServletContext();
        String realPath=sc.getRealPath("").substring(0,sc.getRealPath("").indexOf("\\", sc.getRealPath("").indexOf("\\")+1));
        String projectName=sc.getContextPath().substring(1, sc.getContextPath().length());
        
        //添加系统属性示例代码
        if (org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS) {
           System.setProperty("log.path", realPath+ SystemUtils.FILE_SEPARATOR + projectName+SystemUtils.FILE_SEPARATOR +"logs");
        } else {//linux
           System.setProperty("log.path", "/logs");
        }
        System.setProperty("log.project.name", projectName);
        boolean isProductEnv=false;//非生产环境、
        
        if (isProductEnv) {
           System.setProperty("log.root.level", "INFO");
        } else {//非生产环境
           System.setProperty("log.root.level", "INFO");
        } 
        WebLogbackConfigurer.initLogging(sc);
    }

    @Override
    public void contextDestroyed(ServletContextEvent event){
        WebLogbackConfigurer.shutdownLogging(event.getServletContext());
    }
}
